<?php
declare (strict_types=1);

namespace app\common\model;

use app\common\facade\WeChat;
use app\common\model\Reservation as ReservationModel;

class Reservation extends Base
{
    protected $name = 'reservation';

    public function p()
    {
        return $this->hasOne(Area::class, 'code', 'province')->field('code,province as title');
    }

    public function c()
    {
        return $this->hasOne(Area::class, 'code', 'city')->field('code,city as title');
    }

    public function d()
    {
        return $this->hasOne(Area::class, 'code', 'district')->field('code,district as title');
    }

    public function t()
    {
        return $this->hasOne(Area::class, 'code', 'town')->field('code,town as title');
    }


    public static function PushReservation($param)
    {
        try {
            $route = 'callback/Push_news.log';
            Logs::write($route,'发送模板');
            WeChat::withConfig(config('pay')['wechat_1'])->getAllPrivateTemplate();
//            $message_pusher = explode(',', cache('system')['message_pusher']);

                $wedata = [
                    'touser' =>  cache('system')['message_pusher'],
                    'template_id' => 'FNjONNzHYA31siaVFDA3rWZ5uKvMRYfcOPbwIfArzb0',
                    'url'  => '',
                    'data' => [
                        'first' => [
                            'value' => '收到客户预约通知',
                            'color' => '#173177'
                        ],
                        'keyword1' => [
                            'value' => $param['name'],
                            'color' => '#173177'
                        ],
                        'keyword2' => [
                            'value' => $param['mobile'],
                            'color' => '#173177'
                        ],
                        'keyword3' => [
                            'value' => $param['maintain'],
                            'color' => '#173177'
                        ],
                        'keyword4' => [
                            'value' => $param['desire_time'],
                            'color' => '#173177'
                        ],
                        'keyword5' => [
                            'value' =>  $param['details'],
                            'color' => '#173177'
                        ],
                        'remark' => [
                            'value' => '',
                            'color' => '#173177'
                        ]
                    ]
                ];
                $msg = WeChat::withConfig(config('pay')['wechat_1'])->wechatTemplateSend($wedata);

                Logs::write($route,$msg);

        } catch (\Exception $e) {
            parent::rollback();
            Logs::write($route,$e->getMessage());
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
        //发送模板消息

    }

}
